﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKView;

namespace EditColumn
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class ColumnAlignToWall : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Transaction transaction = null;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				Document document = cmdData.Application.ActiveUIDocument.Document;
				transaction = new Transaction(document);
				transaction.Start("column align to wall");
				AlignOperator alignOperator = new AlignOperator(cmdData);
				if (alignOperator.Operate(ref message) == null)
				{
					transaction.Commit();
				}
				else
				{
					YJKMessageBox.Show(message);
					transaction.RollBack();
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
